package ExamSystemServer.server;

import ExamSystemClient.model.ChoiceQuestion;
import ExamSystemClient.model.Student;
import ExamSystemClient.model.User;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 实现数据的存取
 *
 * @author Shuxian Huang
 */
public class ServerDao {

    /**
     * 实现管理员账号和密码的校验并将结果返回出去
     * @param user
     * @return
     */
    public boolean serverManagerCheck(User user){
        if ("admin".equals(user.getUserName()) &&
                "123456".equals(user.getPassword())){
            return true;
        }
        return false;
    }

    public void serilizeStudentInfo(List<Student> students) throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("studentInfos.txt"));
        oos.writeObject(students);
        oos.close();

        System.out.println("存储学生信息成功！");
    }

    public List<Student> importStudentInfo() throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("studentInfos.txt"));
        List<Student> infos= (List<Student>) ois.readObject();
        ois.close();
        return infos;
    }

    public void serilizeQuestions(List<ChoiceQuestion> questions) throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("QuestionsBank.txt"));
        oos.writeObject(questions);
        oos.close();

        System.out.println("存储学生信息成功！");
    }
    
    public List<ChoiceQuestion> importQuestions() throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("QuestionsBank.txt"));
        List<ChoiceQuestion> infos= (List<ChoiceQuestion>) ois.readObject();
        ois.close();
        return infos;
    }

    public static void main(String[] args) {
//        try {
//            List<Student> users = new ServerDao().importStudentInfo();
//            System.out.println(users);
//        } catch (IOException | ClassNotFoundException e) {
//            e.printStackTrace();
//        }
//        List<Student> users = new ArrayList<>();
//        users.add(new Student(10,"zhangfei", 39));
//        users.add(new Student(11, "liubei", 40));
//        users.add(new Student(12, "guanyu", 43));
//        try {
//            new ServerDao().serilizeStudentInfo(users);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }
}
